<template>
    <div id="tmpl">
    
        <div id="postcomment">
            <h3>提交评论</h3>
            <p class="p"></p>
            <textarea placeholder="请输入您要评论的内容..." v-model="postcontent"></textarea>
            <mt-button type="primary" size="large" @click="postcomment">发表</mt-button>
        </div>
        <!--评论列表获取  -->
        <div id="list">
            <h3>评论列表</h3>
            <p class="p"></p>
            <div v-for="(item,index) in list">
                <div class="title">
                    <span>第{{index+1}}楼:</span>
                    <span>用户：{{item.user_name}}</span>
                    <span>发表时间：{{item.add_time | datefmt('YYYY-MM-DD')}}</span>
                </div>
                <ul class="mui-table-view">
                    <li class="mui-table-view-cell">{{item.content}}</li>
                </ul>
            </div>
        </div>
        <mt-button type="danger" size="large" plain @click="getMore">点击加载更多</mt-button>
    </div>
</template>

 <script type="text/ecmascript-6">
import { Toast } from 'mint-ui'
import common from '../../kits/common.js'
export default {
    props: ['msg'],//接收父组件发来的数据
    data() {
        return {
            pageindex: 1,
            postcontent: '',

            list: []
        }

    },
    created() {
        // this.();
        this.getcommentlist(this.pageindex)
        this.msg = this.$route.params.id
        console.log(this.msg)
    },

    // 请求数据方法

    methods: {
        //发表评论
        postcomment() {
            if (this.postcontent.trim().length <= 0) {
                Toast('输入不能为空')
                return;
            }
            var url = common.apidomain + "/api/postcomment/" + this.msg;
            this.$http.post(url, { content: this.postcontent }, { emulateJSON: true }).then(function (response) {
                Toast(response.body.message);
                this.list = [{ "user_name": "匿名用户", "add_time": new Date(), "content": this.postcontent }].concat(this.list)
                this.postcontent = '';
            })
            // this.getcommentlist(1);
        },
        //获取评论列表
        getcommentlist(pageindex) {
            pageindex = pageindex || 1;
            var url = common.apidomain + "/api/getcomments/" + this.msg + "?pageindex=" + pageindex;
            this.$http.get(url).then(function (response) {
                console.log(response.body)
                if (response.body.status != 0) {
                    Toast(response.body.message)
                    return;
                }
                this.list = this.list.concat(response.body.message);
            })
            console.log(111)
        },
        // 获取更多数据
        getMore() {
            this.pageindex++;
            this.getcommentlist(this.pageindex);
        }

    }
}
</script>

<style scoped >
#postcomment {
    padding: 5px;
}

.p {
    height: 1px;
    width: 100%;
    border-bottom: 1px solid rgba(0, 0, 0, 0.3);
}

#list {
    padding: 5px;
}

.title {
    padding: 5px;
    color: #6d6d72;
    font-size: 15px;
    background-color: rgba(0, 0, 0, 0.1);
}
</style>
